package com.example.holes;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Level08 extends GameEngine {

	public Level08(Resources res){
		super(res);
	}
	
	// ========================
	//   MISSION OBJECTIVE
	// ========================
	@Override
	// describe the mission objective
	protected String getObjective(){
		return "Survive the night!";
	}
	
	@Override
	// specify this level's objective
	protected boolean objectiveComplete() {
		if (timesUp)
			return true;
		else
			return false;
	}
	// ========================
	//   MISSION OBJECTIVE
	// ========================
	
	// ========================
	//   WHICH MOLES AND LOOT
	// ========================
	@Override
	// initialize the images of moles in the level
	protected void InitMolesAndLoot() {
		// moles in level
		moleImages.add(scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.mole00_mole))));
		moleImages.add(scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.mole01_turtle))));
		moleImages.add(scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.mole02_bomb))));
		moleImages.add(scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.mole03_porc0))));
		moleImages.add(scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.mole03_porc1))));
		moleImages.add(scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.mole03_porc2))));
		moleImages.add(scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.mole03_porc3))));
		moleImages.add(scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.mole03_porc4))));
		moleImages.add(scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.mole03_porc5))));
		
		// loot in level
		lootImages[0] = scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.loot_coin)));
		lootImages[1] = scaleImage(Bitmap.createBitmap(BitmapFactory.decodeResource(res, R.drawable.loot_heart)));
	}
	
	@Override
	// what moles can be picked this level
	protected Mole pickMole(int holeNum) {
		Mole mole = null;
		switch(rand.nextInt(4)){
			case 0:	mole = new Mole00_Mole(holeNum, moleImages, 0, 1);
					break;
			case 1: mole = new Mole01_Turtle(holeNum, moleImages, 1, 1);
					break;
			case 2: mole = new Mole02_Bomb(holeNum, moleImages, 2, 1);
					break;
			case 3: mole = new Mole03_Porcupine(holeNum, moleImages, 3, 1);
					break;
		}
		
		return mole;
	}
	
	@Override
	// if loot can be dropped
	protected boolean checkLoot() {
		if (rand.nextInt(2) == 0)
			return true;
		else
			return false;
	}

	@Override
	// what loot to spawn
	protected void spawnLoot(int holeNum) {
		Loot loot = new Loot(holeNum, rand.nextInt(2), lootImages);
		loots[holeNum] = loot;
		holes[holeNum].addLoot(loot);
	}
	// ========================
	//   WHICH MOLES AND LOOT
	// ========================

	// ========================
	//   ADJUST SPAWN RATES
	// ========================
	@Override
	// when timer is up return true and reset timer
	// else return false
	protected boolean spawnTimer() {
		if (count == nextSpawn){
			nextSpawn = nextSpawnMin + nextSpawnConst + rand.nextInt((int)((float)Global.UPS / 2));
			count = 0;
			return true;
		}
		else
			return false;
	}

	@Override
	// adjust spawn timer
	protected void adjustSpawns() {
		if (timer.getTicksLeft() % 400 == 0){
			nextSpawnConst -= 8;
		}
	}

	@Override
	// how to spawn moles
	protected void spawnMole() {
		if (freeHoles > 0){
			// get random empty hole
			int num = randomHole();
			 
			// fill hole with mole
			Mole mole = pickMole(num);
			moles[num] = mole;
			holes[num].fill(mole);
			freeHoles--;
			
			if (freeHoles > 3){
			// spawn a second mole at 1/5 chance
				if (rand.nextInt(5) == 0){
					num = randomHole();
					mole = pickMole(num);
					moles[num] = mole;
					holes[num].fill(mole);
					freeHoles--;
				}
			}
		}
	}
	// ========================
	//   ADJUST SPAWN RATES
	// ========================
}
